package com.heima.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.apis.schedule.IScheduleClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.TaskTypeEnum;
import com.heima.model.schedule.dtos.Task;
import com.heima.model.wemedia.pojos.WmNews;


import com.heima.utils.common.pro.ProtostuffUtil;
import com.heima.wemedia.service.WmNewsAutoScanService;
import com.heima.wemedia.service.WmNewsTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class WmNewsTaskServiceImpl implements WmNewsTaskService {

    @Autowired
    private IScheduleClient scheduleClient;
    @Autowired
    private WmNewsAutoScanService wmNewsAutoScanService;

    /**
     * 添加任务到延迟队列中
     *
     * @param wmNews
     */
    @Override
    @Async
    public void addWmNewsToTask(WmNews wmNews) {
        log.info("添加任务到延迟服务中----begin");
        Task task = new Task();
        task.setExecuteTime(wmNews.getPublishTime().getTime());
        task.setTaskType(TaskTypeEnum.NEWS_SCAN_TIME.getTaskType());
        task.setPriority(TaskTypeEnum.NEWS_SCAN_TIME.getPriority());
        WmNews data = new WmNews();
        data.setId(wmNews.getId());
        byte[] wmnewdata = ProtostuffUtil.serialize(data);
        task.setParameters(wmnewdata);
        scheduleClient.addTask(task);
        log.info("添加任务到延迟服务中----end");
        /*Task task = new Task();
        task.setTaskType(TaskTypeEnum.NEWS_SCAN_TIME.getTaskType());
        task.setPriority(TaskTypeEnum.NEWS_SCAN_TIME.getPriority());
        task.setExecuteTime(wmNews.getPublishTime().getTime());
        WmNews data = new WmNews();
        data.setId(wmNews.getId());
        byte[] serialize = ProtostuffUtil.serialize(data);
        task.setParameters(serialize);
        scheduleClient.addTask(task);*/
    }

    /**
     * 消费延迟队列数据
     */
    @Override
    @Scheduled(fixedRate = 2000)//每2秒刷新一次
    public void scanNewsByTask() throws Exception {
        log.info("开始拉取任务------------");
        //拉取任务
        ResponseResult responseResult = scheduleClient.poll(TaskTypeEnum.NEWS_SCAN_TIME.getTaskType(), TaskTypeEnum.NEWS_SCAN_TIME.getPriority());
        if (null != responseResult.getData() && responseResult.getCode() == 200) {
            Object data = responseResult.getData();
            String Jsondata = JSON.toJSONString(data);
            Task task = JSON.parseObject(Jsondata, Task.class);
            byte[] parameters = task.getParameters();
            WmNews wmNews = ProtostuffUtil.deserialize(parameters, WmNews.class);
            Integer id = wmNews.getId();
            wmNewsAutoScanService.autoScanWmNews(id);
            log.info("开始消费任务,任务id为:{}", id + "---------------");
        }
        log.info("消费完成-------------");
    }
       /* ResponseResult poll = scheduleClient.poll(TaskTypeEnum.NEWS_SCAN_TIME.getTaskType(), TaskTypeEnum.NEWS_SCAN_TIME.getPriority());
        if (null!=poll){
            String datastr = JSON.toJSONString(poll.getData());
            Task task = JSON.parseObject(datastr, Task.class);
            byte[] parameters = task.getParameters();
            WmNews wmNews = ProtostuffUtil.deserialize(parameters, WmNews.class);
            Integer id = wmNews.getId();
            wmNewsAutoScanService.autoScanWmNews(id);
        }
    }*/
    }
